package com.airconditioner.sales.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airconditioner.sales.R;
import com.airconditioner.sales.adapter.BrandAdapter;
import com.airconditioner.sales.adapter.CategroyGridAdapter;
import com.airconditioner.sales.adapter.SubClassAdapter;
import com.airconditioner.sales.model.ModelInfo;
import com.airconditioner.sales.model.ModelInfoResult;
import com.airconditioner.sales.util.StringUtil;
import com.airconditioner.sales.util.Url;
import com.alibaba.fastjson.JSON;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：shuizi_wade on 2017/1/17 17:02
 * 邮箱：674618016@qq.com
 */
@ContentView(R.layout.activity_product_category)
public class ProductCategoryActivity extends BaseActivity {
    @ViewInject(R.id.recy_list)
    RecyclerView recy_list;
    @ViewInject(R.id.recy_grid_list)
    RecyclerView recy_grid_list;
    @ViewInject((R.id.layout_empty))
    RelativeLayout layout_empty;
    private BrandAdapter brandAdapter;
    private CategroyGridAdapter categroyGridAdapter;
    private SubClassAdapter subClassAdapter;
    @ViewInject(R.id.categroy_title)
    TextView categroy_title;
    private List<ModelInfo> modelInfos = new ArrayList<>();

    @Override
    public void init() {
        super.init();
        title_iv_back.setImageResource(R.mipmap.titlebar_back_black);
        title_relate_layout.setBackgroundColor(getResources().getColor(R.color.white_color));
        title_iv_back.setBackgroundColor(getResources().getColor(R.color.white_color));
        title_tv_title.setText(R.string.home_title);
        brandAdapter = new BrandAdapter(context);
        LinearLayoutManager mgr = new LinearLayoutManager(context);
        mgr.setOrientation(LinearLayoutManager.VERTICAL);
        recy_list.setLayoutManager(mgr);
        recy_list.setAdapter(brandAdapter);
        brandAdapter.setOnItemClickListener(new BrandAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion, String name) {
                httpBrand(name);
//                startActivity(new Intent(ProductCategoryActivity.this, InputUserInfoActivity.class));
            }
        });
        layout_empty.setVisibility(View.GONE);
        categroy_title.setVisibility(View.GONE);
        initListSubClassAdapter();
        httpBrand("格力");
    }

    private void httpBrand(String name) {
        RequestParams params = new RequestParams(Url.CategoryList);
        params.addBodyParameter("brand", name);
        httpPost("category", params);
    }

    @Override
    public void success(String result, String type) {
        super.success(result, type);
        if (type.equals("category")) {
            ModelInfoResult modelInfoResult = JSON.parseObject(result, ModelInfoResult.class);
            modelInfos = modelInfoResult.getResObjs();
            subClassAdapter.setmList(modelInfos);
            subClassAdapter.notifyDataSetChanged();
        }
    }


    private void initGridLittleCategroyAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        recy_grid_list.setLayoutManager(gridLayoutManager);
        categroyGridAdapter = new CategroyGridAdapter(context);
        recy_grid_list.setAdapter(categroyGridAdapter);
        categroyGridAdapter.setOnItemClickListener(new CategroyGridAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                initListSubClassAdapter();
            }
        });
    }

    private void initListSubClassAdapter() {
        subClassAdapter = new SubClassAdapter(context);
        LinearLayoutManager mgr = new LinearLayoutManager(context);
        mgr.setOrientation(LinearLayoutManager.VERTICAL);
        recy_grid_list.setLayoutManager(mgr);
        recy_grid_list.setAdapter(subClassAdapter);
        subClassAdapter.setOnItemClickListener(new SubClassAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Bundle bundle = new Bundle();
                bundle.putInt("macId", modelInfos.get(postion).getMacId());
                jumpBundleActvity(ProductDetailsActivity.class, bundle);
            }
        });
    }

    ;

    @Event(value = {R.id.title_iv_back, R.id.categroy_title})
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
            case R.id.categroy_title:
                initGridLittleCategroyAdapter();
                break;
        }
    }
}
